package com.keyin;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.keyin.android.view.ChangeFocusListener;
import com.keyin.android.view.SegmentControl;
import com.keyin.widget.BookShelfWidget;
import com.keyin.widget.KeyinWebviewWidget;
import com.keyin.widget.MagazineListWidget;
import com.keyin.widget.MainWidgetBase;
import com.keyin.widget.NewPeriodicalsWidget;

public class Main extends BaseActivity {
	

	public static final String MAIN_INDEX_KEY = "main_index";

	private LinearLayout container;
	private SegmentControl segment;
	private MainWidgetBase currentView;

	private NewPeriodicalsWidget newMagazinesView;
	private MagazineListWidget magazineList;
	private BookShelfWidget bookShelf;
	private KeyinWebviewWidget webView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.a_main);
		container = (LinearLayout) findViewById(R.id.main_container);

		segment = (SegmentControl) findViewById(R.id.segment_control);
		segment.setChangeFocusListener(new ChangeFocusListener() {

			@Override
			public void onFocus(View v, int position) {
				
				v.setBackgroundColor(Color.BLUE);

				Intent i = new Intent();
				i.setClass(Main.this, Main.class);
				i.putExtra(MAIN_INDEX_KEY, position);
				i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(i);
			}

			@Override
			public void onBlur(View v, int position) {
				v.setBackgroundColor(0XFFC0D9D9);
			}
		});

		onNewIntent(getIntent());
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);

		init(intent);
	}

	private void init(Intent intent) {
		int index = intent.getIntExtra(MAIN_INDEX_KEY, -1);
		if (index != -1) {
			segment.changeFocus(index);
		} else {
			index = 0;
			segment.changeFocus(index);
		}

		currentView = getView(index);
		currentView.onNewIntent(intent);

		container.removeAllViews();
		container.addView((View) currentView);
	}

	private MainWidgetBase getView(int index) {
		if (index == 0) {
			if (newMagazinesView == null) {
				newMagazinesView = new NewPeriodicalsWidget(this);
			}
			return newMagazinesView;
		} else if (index == 1) {
			if (magazineList == null){
				magazineList = new MagazineListWidget(this);
			}
			return magazineList;
		} else if (index == 2) {
			if (bookShelf == null){
				bookShelf = new BookShelfWidget(this);
			}
			return bookShelf;
		} else if (index == 3){
//			Intent viewIntent = new Intent("android.intent.action.VIEW",Uri.parse("http://www.keyin.cn"));
//			startActivity(viewIntent);
//			if (webView == null){
//				webView = new KeyinWebviewWidget(this);
//			}
//			return webView;
			
			startActivity(new Intent(this, Web.class));
		}
		return newMagazinesView;
	}

	@Override
	protected void onPause() {
		super.onPause();
	}
}
